import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

import 'health_data_model.dart';

class HealthDataModule extends StatefulWidget {
  const HealthDataModule(
      {super.key,
      required this.name,
      required this.summaryTitle,
      required this.runHealthData});

  final String name;
  final Future<HealthDataModel> runHealthData;
  final String summaryTitle;

  @override
  State<HealthDataModule> createState() => _HealthDataModuleState();
}

class _HealthDataModuleState extends State<HealthDataModule> {
  final FlLine _flLine =
      const FlLine(color: Colors.black26, dashArray: [5, 8], strokeWidth: 1);

  final LinearGradient _linearGradient = const LinearGradient(
      begin: Alignment.bottomCenter,
      end: Alignment.topCenter,
      colors: [Colors.yellowAccent, Colors.blue]);

  List<BarChartGroupData>? getBarChartGroupData(HealthDataModel healthData) {
    List<BarChartGroupData> result = [];
    for (int idx = 0; idx < healthData!.curveData!.length; idx++) {
      CurveData curveData = healthData!.curveData![idx];
      result.add(BarChartGroupData(
          x: idx, barRods: getBarChatRodData(curveData.curveDateItems ?? [])));
    }
    return result;
  }

  List<BarChartRodData>? getBarChatRodData(List<CurveDateItems> items) {
    return items.map((e) {
      return BarChartRodData(
          toY: e.value!.toDouble(),
          gradient: LinearGradient(
              colors: [Colors.yellowAccent, Colors.blue],
              begin: Alignment.bottomCenter,
              end: Alignment.topCenter));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      padding: EdgeInsets.symmetric(horizontal: 20),
      children: [
        Column(
          children: [
            Container(
                child: Column(
              children: [
                FutureBuilder<HealthDataModel?>(
                  future: widget.runHealthData,
                  builder: (ctx, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return const Center(
                          child: Text(
                        "loadin...",
                      ));
                    }
                    HealthDataModel? healthData = snapshot.data;
                    return Container(
                      margin: EdgeInsets.only(top: 10),
                      padding: EdgeInsets.only(top: 10),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20)),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(widget.summaryTitle,
                              style: TextStyle(color: Colors.black54)),
                          Wrap(
                            spacing: 10,
                            crossAxisAlignment: WrapCrossAlignment.center,
                            children: [
                              Text(
                                healthData?.stepCnt.toString() ?? "0",
                                style: TextStyle(
                                    fontSize: 20, fontWeight: FontWeight.bold),
                              ),
                              Text("步", style: TextStyle(color: Colors.black54))
                            ],
                          ),
                          Text(
                            healthData?.timeScope ?? "",
                            style: TextStyle(color: Colors.black54),
                          ),
                          Container(
                            height: 210,
                            child: BarChart(
                              BarChartData(
                                  gridData: FlGridData(
                                    show: true,
                                    getDrawingHorizontalLine: (value) {
                                      return _flLine;
                                    },
                                    getDrawingVerticalLine: (value) {
                                      return _flLine;
                                    },
                                  ),
                                  alignment: BarChartAlignment.spaceAround,
                                  maxY: healthData?.maxValue?.toDouble() ?? 100,
                                  barTouchData: BarTouchData(
                                    enabled: true,
                                  ),
                                  titlesData: FlTitlesData(
                                      leftTitles: const AxisTitles(
                                        sideTitles: SideTitles(
                                          showTitles: false,
                                        ),
                                      ),
                                      topTitles: const AxisTitles(
                                          sideTitles:
                                              SideTitles(showTitles: false)),
                                      bottomTitles: AxisTitles(
                                          sideTitles: SideTitles(
                                              showTitles: true,
                                              interval: 5,
                                              reservedSize: 30,
                                              getTitlesWidget: (value, data) {
                                                return Text("${value}");
                                              }))),
                                  borderData: FlBorderData(
                                      show: true,
                                      border: Border.all(
                                        color: Colors.black12,
                                        width: 1,
                                        style: BorderStyle.solid,
                                      )),
                                  groupsSpace: 30,
                                  barGroups: getBarChartGroupData(healthData!)),
                              swapAnimationDuration:
                                  const Duration(microseconds: 500),
                              swapAnimationCurve: Curves.easeInOut,
                            ),
                          )
                        ],
                      ),
                    );
                  },
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("摘要"),
                    TextButton(onPressed: () {}, child: Text("显示全部"))
                  ],
                ),
                Container(
                    height: 150,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20))),
                const SizedBox(
                  height: 20,
                ),
                Container(
                    height: 150,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20))),
                const SizedBox(
                  height: 20,
                ),
                Container(
                    height: 150,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(20))),
              ],
            ))
          ],
        )
      ],
    );
  }
}
